//
//  CustomOrderPayTableViewCell.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/3/4.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "CustomOrderPayTableViewCell.h"
#import "OrderDetailViewController.h"
#import "OrderDetailModel.h"
#import <AlipaySDK/AlipaySDK.h>
@implementation CustomOrderPayTableViewCell

- (void)awakeFromNib {
    // Initialization code
    self.payButoon.backgroundColor=[UIColor colorWithHexString:@"f35959"];
    self.payButoon.layer.cornerRadius=25;
    self.payButoon.clipsToBounds=YES;
    self.cancleButtom.backgroundColor=[UIColor colorWithHexString:@"cccccc"];
    self.cancleButtom.layer.cornerRadius=25;
    self.cancleButtom.clipsToBounds=YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)cancleAction:(id)sender {
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"确定取消订单吗？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];

}

- (IBAction)payAction:(id)sender {
    OrderDetailViewController *vc=(OrderDetailViewController*)[CustomOrderPayTableViewCell viewController:self];
    OrderDetailDataModel *orderModel=vc.orderModel;
    //支付
    [[LKDHud shareHud] showLoadingView];
    [ApiClient orderPayOrder_id:orderModel.order_id didFinishLoaded:^(OrderPayModel *model, NSString *path) {
        [[LKDHud shareHud] hideLoadingView];
        if ([model.status isEqual:@(1)]) {
            [[AlipaySDK defaultService] payOrder:model.data.payment_parameter fromScheme:@"alipay.chunlaizhuang" callback:^(NSDictionary *resultDic) {
                if(self.payResultProcessBlock) {
                    self.payResultProcessBlock(resultDic);
                }
            }];
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
        [[LKDHud shareHud] hideLoadingView];
    }];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==1) {
        OrderDetailViewController *vc=(OrderDetailViewController*)[CustomOrderPayTableViewCell viewController:self];
        [ApiClient cancelOrder_ID:vc.orderID didFinishLoaded:^(KXJson *json, NSString *path) {
            [self performSelector:@selector(reloadOrder) withObject:nil afterDelay:0];
            [[LKDHud shareHud] showtip:[json getStringForKey:@"message"]];
            
        } didFailLoaded:^(NSError *error, NSString *path) {
            
        }];
    }
}
-(void)reloadOrder{
    self.payButoon.backgroundColor=[UIColor colorWithHexString:@"cccccc"];
    [self.payButoon setTitle:@"已取消" forState:UIControlStateNormal];
    self.payButoon.enabled=NO;
    self.cancleButtom.hidden=YES;
}

@end
